package com.bw.home.ui

import android.R.attr.thumb
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.home.adpter.SimpleVideoAdapter
import com.bw.home.databinding.FragmentSimpleTypeBinding
import com.bw.home.entity.SimpleVideoEntity
import com.bw.home.intent.SimpleVideoIntent
import com.bw.home.state.SimpleVideoState
import com.bw.home.viewmodel.SimpleVideoViewModel
import com.bw.lib_common.ConstValue
import com.bw.lib_mvi.view.BaseMVIFragment
import com.umeng.socialize.ShareAction
import com.umeng.socialize.media.UMWeb
import kotlinx.coroutines.launch


class SimpleTypeFragment : BaseMVIFragment() {
    lateinit var binding: FragmentSimpleTypeBinding
    lateinit var viewmodle: SimpleVideoViewModel

    var channel: String = ""
    var page: Int = 0
    var pageSize: Int = 10

    lateinit var adapter: SimpleVideoAdapter
    val list: MutableList<SimpleVideoEntity> = mutableListOf()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentSimpleTypeBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun generateViewModel() {
        viewmodle = ViewModelProvider(this).get(SimpleVideoViewModel::class.java)
    }

    override fun initView() {
        super.initView()
        adapter = SimpleVideoAdapter(requireContext(), list)
        binding.simpleTypeRv.adapter = adapter
    }

    override fun initData() {
        super.initData()
        channel = arguments?.getString("channelId").toString()
        loadSimpleVideo()
    }

    override fun initEvent() {
        super.initEvent()
        adapter.setPlayOnClickListener(object : SimpleVideoAdapter.PlayOnClickListener {
            override fun onClick(entity: SimpleVideoEntity) {
                val boolean = SPUtils.getInstance().getBoolean("login", false)
                if (boolean) {
                    Toast.makeText(requireContext(), entity.title, Toast.LENGTH_LONG).show()
                    ARouter.getInstance().build(ConstValue.HOME_DETAIL_PATH)
                        .withParcelable(ConstValue.homeDetailData, entity).navigation()
                } else {
                    ToastUtils.showLong("请先登录")
                    ARouter.getInstance().build(ConstValue.LOGIN).navigation()
                }
            }
        })

        adapter.setShareOnClickListener(object : SimpleVideoAdapter.ShareOnClickListener {
            override fun onClick(entity: SimpleVideoEntity) {
                //分享视频地址
                val web = UMWeb(entity.videopath)
                web.title = "This is music title" //标题
                web.description = "my description" //描述

                ShareAction(requireActivity())
                    .withMedia(web)
                    .open();
            }
        })
    }

    private fun loadSimpleVideo() {
        lifecycleScope.launch {
            viewmodle.intents.send(SimpleVideoIntent.getSimpleVideoIntent(channel, page, pageSize))
        }
    }

    override fun handlerState() {
        lifecycleScope.launch {
            viewmodle.state.collect {
                when (it) {

                    is SimpleVideoState.getSimpleVideoState -> {
                        list.clear()
                        list.addAll(it.data)
                        adapter.notifyDataSetChanged()
                    }

                    is SimpleVideoState.Failed -> {
                        Log.d("TagA", "请求失败: $it")
                    }

                    else -> {}
                }
            }
        }
    }

}